package com.isunland.managesystem.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.PlanDetail;
import java.util.List;

@SuppressLint("ResourceAsColor") public class DelegateTaskListAdapter
    extends ArrayAdapter<PlanDetail> {

  private LayoutInflater mInflater;

  // Constructors
  public DelegateTaskListAdapter(Context context, List<PlanDetail> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  @NonNull @Override
  public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_delegate_task, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    PlanDetail item = getItem(position);
    vh.tvDelegateTask.setText(item != null ? item.getPlanContentDesc() : ""); // 委派任务内容
    vh.tvDelegateTime.setText(getContext().getString(R.string.requestFinishTimeWithHolder,
        (item != null) ? item.getPlanEsteTime() : ""));
    // 委派任务要求完成时间
    vh.tvexcManName.setText(item != null ? item.getExcManName() : null);   //委派任务执行人
    vh.tvrateCurrent.setText(
        ((item == null) || (item.getRateCurrent() == null)) ? "0%" : item.getRateCurrent());
    //已用工时
    vh.tvWorkHour.setText(getContext().getString(R.string.hourWithHolder,
        ((item == null) || (item.getWorkTime() == null)) ? "0" : item.getWorkTime()));
    //noinspection deprecation
    vh.tvexcManName.setTextColor(getContext().getResources()
        .getColor(((item != null) && ("backeval".equalsIgnoreCase(item.getEvaStatus())
            || "登记退回".equalsIgnoreCase(item.getPlanStatus()))) ? R.color.red : R.color.primary));
    return vh.rootView;
  }

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-08-17 14:58:09 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    final TextView tvDelegateTask;
    final TextView tvDelegateTime;
    final TextView tvrateCurrent;
    final TextView tvexcManName;
    final TextView tvWorkHour;

    private ViewHolder(LinearLayout rootView, TextView tvDelegateTask, TextView tvDelegateTime,
        TextView tvrateCurrent, TextView tvexcManName, TextView tvWorkHour) {
      this.rootView = rootView;
      this.tvDelegateTask = tvDelegateTask;
      this.tvDelegateTime = tvDelegateTime;
      this.tvrateCurrent = tvrateCurrent;
      this.tvexcManName = tvexcManName;
      this.tvWorkHour = tvWorkHour;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvDelegateTask = (TextView) rootView.findViewById(R.id.tv_delegateTask);
      TextView tvDelegateTime = (TextView) rootView.findViewById(R.id.tv_delegateTime);
      TextView tvrateCurrent = (TextView) rootView.findViewById(R.id.tv_rateCurrent);
      TextView tvexcManName = (TextView) rootView.findViewById(R.id.tv_excManName);
      TextView tvWorkHour = (TextView) rootView.findViewById(R.id.tv_usedHour);
      return new ViewHolder(rootView, tvDelegateTask, tvDelegateTime, tvrateCurrent, tvexcManName,
          tvWorkHour);
    }
  }

  //public DelegateTaskListAdapter(Context context,
  //		DelegateTaskContent[] objects) {
  //	super(context, 0, objects);
  //	this.mInflater = LayoutInflater.from(context);
  //}
}
